<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/22 0022
 * Time: 11:19
 */

namespace app\api\controller\v1;


use app\currency\service\WechatTemplateService;
use app\wechat\service\WechatService;
use library\Controller;
use think\Db;

class WxNotify extends Controller
{
    /**
     * 微信支付回调
     * @param $id
     * @return string
     * @throws \WeChat\Exceptions\InvalidResponseException
     */
    public function read($id)
    {
        file_put_contents('wx_pay_log.log',$id.'回调开始:'.PHP_EOL,FILE_APPEND);
        $wechat = WechatService::WePayOrder();
        $notify = $wechat->getNotify();
        file_put_contents('wx_pay_log.log','数据:'.json_encode($notify).PHP_EOL,FILE_APPEND);
        if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
            if (method_exists(self::class,$id) && $this->$id($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, 'wechat')) {
                return $wechat->getNotifySuccessReply();
            }
        } else {
            return $wechat->getNotifySuccessReply();
        }
    }

    /**
     * 积分充值订单状态更新
     * @param string $order_no
     * @param string $pay_no
     * @param string $pay_price
     * @param string $type
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function integral($order_no, $pay_no, $pay_price, $type = 'wechat')
    {
        // 检查订单支付状态
        $where = ['order_no' => $order_no, 'pay_state' => '0', 'status' => '3'];
        $order = Db::name('MemberIntegralOrder')->where($where)->find();
        if (empty($order)) return false;
        // 更新订单支付状态
        $result = Db::name('MemberIntegralOrder')->where($where)->update([
            'pay_state' => 1,
            'pay_type'  => $type,
            'pay_price' => $pay_price,
            'pay_no' => $pay_no,
            'pay_at' => date('Y-m-d H:i:s'),
        ]);
        // 给用户增加积分的操作写在MYSQL触发器里
        return $result !== false;
    }

    /**
     * 服务订单状态更新
     * @param string $order_no 订单号
     * @param string $pay_no 交易号
     * @param string $pay_price 交易金额
     * @param string $type 支付类型
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    private function clean($order_no, $pay_no, $pay_price, $type = 'wechat')
    {
        // 检查订单支付状态
        $where = ['order_no' => $order_no, 'pay_state' => '0', 'status' => '3'];
        $order = Db::name('CleanOrder')->where($where)->find();
        if (empty($order)) return false;
        // 更新订单支付状态
        $result = Db::name('CleanOrder')->where($where)->update([
            'pay_type'  => $type,
            'pay_no' => $pay_no,
            'status' => 4,
            'pay_price' => $pay_price,
            'pay_state' => 1,
            'pay_at' => date('Y-m-d H:i:s'),
        ]);

        if($result !== false){
            //订单付款成功提醒
            $order = Db::name('CleanOrder')->where(['order_no' => $order_no])->find();
            $openid = Db::name('Cleaner')->where('id',$order['cid'])->value('openid');
            WechatTemplateService::sendCleanerOrderPayed($openid,$order_no, $order['contact_name'], $order['total_price'], date('Y-m-d H:i:s'));
        }
        return $result !== false;
    }
}